package com.GoldMiner.util;

import java.sql.*;

/**
 * 数据库工具类
 * 本源码由上玄开源，转载附带版权信息。
 */

public class DataBase {
    Statement state = null;
    Connection conn = null;
    PreparedStatement ps = null;
    private String username;//用户名
    private String password;//密码
    private int max_record = 0;//最高记录关数
    public static int level = 1; //当前关卡
    public static int goal = level * 10; //目标分数

    public DataBase() throws Exception {
        //注册驱动(mysql5后可省略)
        Class.forName("com.mysql.cj.jdbc.Driver");
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/game", "root", "123456");
        if (conn != null) {// 如果数据库不为空
            System.out.println("数据库连接成功！");
            System.out.println(conn);
            this.conn = conn;
        }
        this.state = conn.createStatement();
    }

    public DataBase(String username, String password) {
        //注册方法
        this.username = username;
        this.password = password;
    }

    //关闭数据库
    public void close() throws Exception {
        if (state != null) {
            state.close();
        }
        if (conn != null) {
            conn.close();
        }
    }

    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    //获取用户密码
    public String getPassword(String username) {
        String sql = "SELECT password FROM users WHERE username = ?;";
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, username);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                password = rs.getString("password");
            }
            rs.close();
            return password;
        } catch (Exception e) {
            e.printStackTrace();
            return null;  // 处理异常并返回 null
        } finally {
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //设置password
    public void setPassword(String password) {
        this.password = password;
    }

    //获取最高纪录
    public int getMax_record() {
        String sql = "SELECT max_record FROM users WHERE username = ?;";
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, username);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                max_record = rs.getInt("max_record");
            }
            rs.close();
            return max_record;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;  // 处理异常并返回 null
        } finally {
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    // 设置最高纪录
    public void setMaxRecord(int newMaxRecord) {
        String sql = "UPDATE users SET max_record = ? WHERE username = ?;";
        try (PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, newMaxRecord); // 设置新的最高纪录
            ps.setString(2,username);  // 设置用户名
            int rowsAffected = ps.executeUpdate(); // 执行更新操作

            //可以根据需要添加对更新行数的检查
            if (rowsAffected > 0) {
                //更新成功
                System.out.println("用户 " + username + " 的最高纪录已更新为 " + newMaxRecord);
            } else {
                //没有更新
                System.out.println("用户 " +username + " 不存在或最高纪录未改变");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public String toString() {
        return "DataBase{state = " + state + ", conn = " + conn + ", ps = " + ps + ", username = " + username + ", password = " + password + ", max_record = " + max_record + "}";
    }

    public void register( String username,String password) {
        String sql = "INSERT INTO users (username,password) VALUES (?,?);";
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, username);
            ps.setString(2, password);
            ps.executeUpdate();
            System.out.println("注册成功！");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
